package moo.stargalaxy_bungee.api;

import moo.stargalaxy_bungee.GalaxyBunGee;
import moo.stargalaxy_bungee.config.Config;
import moo.stargalaxy_bungee.data.StarGalaxyData;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Map;

public class Cmd extends Command {

    public Cmd() {
        super("sgbc");
    }

    @Override
    public void execute(CommandSender commandSender, String[] strings) {
        if(strings.length == 0 && commandSender.hasPermission("galaxy.cmd")){
            commandSender.sendMessage(new TextComponent("§f┃ "));
            commandSender.sendMessage(new TextComponent("§f┃  §6StarGalaxy §c(群星插件帮助-蹦极版)"));
            commandSender.sendMessage(new TextComponent("§f┃  §8Version §7" + Config.version));
            commandSender.sendMessage(new TextComponent("§f┃ "));
            commandSender.sendMessage(new TextComponent("§f┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"));
            commandSender.sendMessage(new TextComponent("§f┃ "));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc reload  §8━━▶ §7重载配置和群星链接"));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc listserver  §8━━▶ §7查看当前全部子服信息"));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc gosp §e<玩家名字> §e<子服名字>  §8━━━▶ §7特定玩家跨入子服务器"));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc gosa §e<子服名字>  §8━━▶ §7全体玩家跨入子服务器"));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc sendp §e<玩家名字> §e<消息文本>  §8━━▶ §7特定玩家发送消息文本"));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc senda §e<消息文本>  §8━━▶ §7全体玩家发送消息文本"));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc addserver §e<子服名字> §e<链接地址>  §8━━▶ §7手动热添加蹦极子服"));
            commandSender.sendMessage(new TextComponent("§f┃  §a/sgbc delserver §e<子服名字>  §8━━▶ §7手动热删除蹦极子服"));
            commandSender.sendMessage(new TextComponent("§f┃ "));
            return;
        }

        // 功能：给全部玩家发送文本消息内容&合并包含空格文本内容
        if(strings[0].equals("senda") && commandSender.hasPermission("galaxy.senda")){
            StringBuilder builder = new StringBuilder();
            if(strings.length > 2){
                for (int i = 0; i < strings.length; i++) {
                    if(i >= 1){
                        builder.append(strings[i]).append(" ");
                    }
                }
                for (ProxiedPlayer player : GalaxyBunGee.getPlugin().getProxy().getPlayers()) {
                    player.sendMessage(new TextComponent(String.valueOf(builder)));
                }
            } else {
                for (ProxiedPlayer player : GalaxyBunGee.getPlugin().getProxy().getPlayers()) {
                    player.sendMessage(new TextComponent(strings[1]));
                }
            }
            return;
        }

        // 功能：重载配置
        if(strings.length == 1){
            if(strings[0].equals("reload") && commandSender.hasPermission("galaxy.reload")){
                Config.loadConfig();
                GalaxyBunGee.getPlugin().getLogger().info("§f配置重载完成 §7- " + GalaxyBungeeAPI.intiGalaxyAPI());
                return;
            }
            if(strings[0].equals("listserver") && commandSender.hasPermission("galaxy.listserver")){
                for (Map.Entry<String, ServerInfo> stringServerInfoEntry : GalaxyBunGee.getPlugin().getProxy().getServers().entrySet()) {
                    ServerInfo value = stringServerInfoEntry.getValue();
                    boolean b = value.canAccess(commandSender);
                    if(b){
                        commandSender.sendMessage(new TextComponent("§7服务器：§a" + stringServerInfoEntry.getKey()
                                + " §8玩家：§e" + value.getPlayers().size()
                                + " §8连接地址：§3" + value.getAddress()
                                + " §8状态：§a正常"
                        ));
                    } else {
                        commandSender.sendMessage(new TextComponent("§7服务器：§a" + stringServerInfoEntry.getKey()
                                + " §7人数：§e" + value.getPlayers().size()
                                + " §7连接地址：§3" + value.getAddress()
                                + " §7状态：§c异常"
                        ));
                    }
                }
                return;
            }
        }

        // 功能：给全部玩家跨到特定子服务器
        if(strings.length == 2){
            if(strings[0].equals("gosa") && commandSender.hasPermission("galaxy.gosa")){
                if(GalaxyBunGee.getPlugin().getProxy().getServerInfo(strings[1]) != null){
                    ServerInfo serverInfo = GalaxyBunGee.getPlugin().getProxy().getServerInfo(strings[1]);
                    for (ServerInfo value : GalaxyBunGee.getPlugin().getProxy().getServers().values()) {
                        if(serverInfo.equals(value)){
                            continue;
                        }
                        for (ProxiedPlayer player : value.getPlayers()) {
                            if(!GalaxyBungeeAPI.addTpCoolDown(player)){
                                player.sendMessage(new TextComponent(Config.getConfiguration().getString("Message_Set.GoServer_Delay")));
                                continue;
                            }
                            player.connect(serverInfo);
                        }
                    }
                    GalaxyBunGee.getPlugin().getLogger().info("§7已操作 §6全体玩家§7 传送至服务器 §a" + strings[1] );
                } else {
                    GalaxyBunGee.getPlugin().getLogger().info("指定服务器 " + strings[1] + " 不存在");
                }
            }

            // 功能：删除蹦极端内特定子服务器
            if(strings[0].equals("delserver") && commandSender.hasPermission("galaxy.delserver")){
                if(!StarGalaxyData.getServerInfoMap().containsKey(strings[1])){
                    commandSender.sendMessage(new TextComponent("§c指定服务器：§6" + strings[1] + "§c 不存在.删除失败"));
                    // GalaxyBunGee.getPlugin().getLogger().info("§c指定服务器 §6" + strings[1] + "§c 不存在.删除失败")
                    return;
                }
                StarGalaxyData.getServerInfoMap().remove(strings[1]);
                ProxyServer.getInstance().getServers().remove(strings[1]);
                commandSender.sendMessage(new TextComponent("§a指定服务器：§6" + strings[1] + "§a 已经移除"));
                // GalaxyBunGee.getPlugin().getLogger().info("§a指定服务器 §6" + strings[1] + "§a 已经移除")
            }
        }

        // 功能：给特定玩家跨到特定子服务器
        if(strings.length == 3){
            if(strings[0].equals("gosp") && commandSender.hasPermission("galaxy.gosp")){
                if(GalaxyBunGee.getPlugin().getProxy().getServerInfo(strings[2]) != null && GalaxyBunGee.getPlugin().getProxy().getPlayer(strings[1]) != null){
                    ProxiedPlayer player = GalaxyBunGee.getPlugin().getProxy().getPlayer(strings[2]);
                    // 判断跨服是否冷却中
                    if(!GalaxyBungeeAPI.addTpCoolDown(player)){
                        player.sendMessage(new TextComponent(Config.getConfiguration().getString("Message_Set.GoServer_Delay")));
                        return;
                    }
                    player.connect(GalaxyBunGee.getPlugin().getProxy().getServerInfo(strings[1]));
                    GalaxyBunGee.getPlugin().getLogger().info("§7已操作 §6" + strings[1] + "§7 传送至服务器 §a" + strings[2] );
                } else {
                    GalaxyBunGee.getPlugin().getLogger().info("§c选定的子服务器或者玩家不存在，请检查输入参数内容");
                }
            }

            // 功能：给特定玩家发送文本消息
            if(strings[0].equals("sendp") && commandSender.hasPermission("galaxy.sendp")){
                if(GalaxyBunGee.getPlugin().getProxy().getPlayer(strings[1]) != null){
                    GalaxyBunGee.getPlugin().getProxy().getPlayer(strings[1]).sendMessage(new TextComponent(strings[2]));
                } else {
                    GalaxyBunGee.getPlugin().getLogger().info("§c选定的子服务器或者玩家不存在，请检查输入参数内容");
                }
                return;
            }

            // 功能：添加蹦极端内新子服务器
            if(strings[0].equals("addserver") && commandSender.hasPermission("galaxy.addserver")){
                if(StarGalaxyData.getServerInfoMap().containsKey(strings[1])){
                    commandSender.sendMessage(new TextComponent("§a指定服务器：§6" + strings[1] + "§a 已存在,请勿重复添加"));
                    // GalaxyBunGee.getPlugin().getLogger().info("§c指定服务器 §6" + strings[1] + "§c 已存在,无需重复添加");
                    return;
                }
                InetSocketAddress address;
                if(strings[2].split(":").length == 1){
                    address = new InetSocketAddress(strings[2].split(":")[0], 25565);
                } else {
                    address = new InetSocketAddress(strings[2].split(":")[0], Integer.parseInt(strings[2].split(":")[1]));
                }
                ServerInfo serverInfo = ProxyServer.getInstance().constructServerInfo(strings[1], address, "From Plugin StarGalaxy-Bungee", false);
                StarGalaxyData.getServerInfoMap().put(strings[1], serverInfo);
                GalaxyBunGee.getPlugin().getProxy().getConfig().getServers().put(strings[1], serverInfo);
                GalaxyBunGee.getPlugin().getProxy().getConfigurationAdapter().getServers().put(strings[1], serverInfo);
                commandSender.sendMessage(new TextComponent("§a指定服务器：§6" + strings[1] + "§a 已经添加"));
                // GalaxyBunGee.getPlugin().getLogger().info("§a指定服务器 §6" + strings[1] + "§a 已经添加");
            }
        }

        // 功能：合并包含空格文本内容&发送给单独玩家
        if(strings.length > 3 && strings[0].equals("sendp") && commandSender.hasPermission("galaxy.sendp")){
            if(GalaxyBunGee.getPlugin().getProxy().getPlayer(strings[1]) != null){
                StringBuilder message = new StringBuilder();
                for (int i = 0; i < strings.length; i++) {
                    if(i >= 2){
                        message.append(" ").append(strings[i]);
                    }
                }
                GalaxyBunGee.getPlugin().getProxy().getPlayer(strings[1]).sendMessage(new TextComponent(String.valueOf(message)));
            } else {
                GalaxyBunGee.getPlugin().getLogger().info("§c选定的玩家不存在，请检查输入参数内容");
            }
        }
    }
}
